package edu.utexas.esel.jgraphpad.archpadplugin.model;

import java.util.HashSet;
import java.util.Set;


/**
 * Manger responsible for generating unique IDs for {@link Element}s.
 *
 * @author svetoslavganov@mail.utexas.edu
 *
 */
public class IdManager {

	private static IdManager sInstance;

	private int mNextId;

	private Set<Integer> mReservedIds = new HashSet<Integer>();

	public static IdManager getInstance() {
		if (sInstance == null) {
			sInstance = new IdManager();
		}
		return sInstance;
	}

    private IdManager() {
    	/* hide constructor */
    }

	/**
	 * Generates a unique id.
	 * @return
	 */
	public int generateId() {
        do {
        	mNextId++;
        } while (mReservedIds.contains(mNextId));
        return mNextId;
	}

	/**
	 * Reserves an ID, i.e. this id will not be generated.
	 *
	 * @param id The id to reserve.
	 * @return
	 */
	public boolean reserveId(Integer id) {
		return mReservedIds.add(id);
	}
}
